package com.daqsoft.controller.system;

import com.daqsoft.data.User;
import com.daqsoft.dto.GeneralResultEnum;
import com.daqsoft.permission.Verify;
import com.daqsoft.permission.VerifyType;
import com.daqsoft.service.system.UserManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author 周麟东 on 16:18
 */
@RestController
@RequestMapping("userManager")
@Verify(desc = "用户管理")
public class UserManageController {

    @Autowired
    private UserManageService managerService;

    /**
     * 新增或更新user
     * @return user对象
     */
    @RequestMapping("saveUser")
    @Verify(value = VerifyType.HAVE_PERMISSION,desc = "保存用户")
    public GeneralResultEnum saveUser(User user , int groupId , int pid){
        return GeneralResultEnum.SUCCESS.setData(managerService.save(user,groupId,pid).toMap()).setDesc("保存成功");
    }
    @RequestMapping("getUser")
    @Verify(value = VerifyType.HAVE_PERMISSION,desc = "获取某个用户")
    public GeneralResultEnum getUser(int id){
        return GeneralResultEnum.SUCCESS.setData(managerService.findOne(id).toMap()).setDesc("获取成功");
    }

    /**
     * 删除用户
     * @param id 用户ID
     */
    @RequestMapping("removeUser")
    @Verify(value = VerifyType.HAVE_PERMISSION,desc = "删除用户")
    public GeneralResultEnum removeUser(int id){
        managerService.remove(id);
        return GeneralResultEnum.SUCCESS;
    }

    /**
     * 获取全部用户
     */
    @RequestMapping("getAll")
    @Verify(value = VerifyType.HAVE_PERMISSION,desc = "获取全部用户")
    public GeneralResultEnum getAllUsers(HttpServletRequest request ,Integer groupId , Boolean leader , boolean haveSelf){
        User self = (User) request.getSession().getAttribute("user");
        List<Map<String,Object>> list = new ArrayList<>();
        List<User> users;
        if (groupId != null && groupId != -1){
            //如果有部门经理或者操作者为部门经理 则返回该部门的所有人员
            users = managerService.getUsersByGroupId(groupId);
//            if (self.getUserGroup().getLeader() ==null){//如果部门经理为空则返回所有人
//
//            }else if(self.getUserGroup().getLeader().getId() == self.getId()){//如果他本人是部门经理则返回该部门的所有人
//                users = managerService.getUsersByGroupId(groupId);
//            }else if(haveSelf) {//如果有自己则返回自己
//                users.add(self);
//            }else { //如果都没有就返回他自己
//                users = managerService.getUsersByGroupId(groupId);
//            }
        }else { // 如果没有指定用户组则返回全部用户
            users = managerService.getAllUsers();
        }
        if (leader!=null && leader){
            Iterator<User> iterator = users.iterator();
            while (iterator.hasNext()){
                User user = iterator.next();
                if (haveSelf && self.getId() == user.getId()){
                    continue;
                }
                if (user.getUserGroup().getLeader() != null && user.getUserGroup().getLeader().getId() == user.getId()){
                    continue;
                }
                iterator.remove();
            }
        }
        for (User user : users) {
            list.add(user.toMap());
        }
        return GeneralResultEnum.SUCCESS.setData(list).setDesc("获取用户成功");
    }


}
